#!/usr/bin/python
# coding:utf-8

import urllib
import urllib2
import ssl
import re
import time
import sqlite3
import os
import thread
import sys
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
import atexit


def daemonize(pid_file=None,work_dir=os.getcwd()):
    pid = os.fork()
    if pid:
        sys.exit(0)
    os.chdir(work_dir)
    os.umask(0)
    os.setsid()
    _pid = os.fork()
    if _pid:
        sys.exit(0)
    sys.stdout.flush()
    sys.stderr.flush()
    with open('/dev/null') as read_null, open('/dev/null', 'w') as write_null:
        os.dup2(read_null.fileno(), sys.stdin.fileno())
        os.dup2(write_null.fileno(), sys.stdout.fileno())
        os.dup2(write_null.fileno(), sys.stderr.fileno())
    if pid_file:
        with open(pid_file, 'w+') as f:
            f.write(str(os.getpid()))
        atexit.register(os.remove, pid_file)

def getHtml(url):
    try:
        context = ssl._create_unverified_context()
        header = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:48.0) Gecko/20100101 Firefox/48.0"}
        request = urllib2.Request(url=url, headers=header)
        response = urllib2.urlopen(request, context=context)
        text = response.read()
        return text
    except Exception, arg:
        print arg
        return "error"


def getPornPage(keyword, page):
    url = "http://www.pornhub.com/video/search?"
    textmod = {'search': keyword, 'page': page}
    textmod = urllib.urlencode(textmod)
    url = url + textmod
    pornPage = getHtml(url)
    if pornPage == "error":
        while pornPage == "error":
            time.sleep(3)
            pornPage = getHtml(url)
    return pornPage


def getPornList(html):
    pat = re.compile(
        r'<a href="/view_video\.php\?viewkey=[\s\S]{0,120}class="img" data-related-url="[\s\S]{0,300}data-mediumthumb="[\s\S]{0,120}\.jpg"')
    return pat.findall(html)


def getPornTitle(listItem):
    pat = re.compile(r'title=".* class="img"')
    title = pat.findall(listItem)
    endPos = len(title[0]) - 13
    return title[0][7:endPos]


def getPornUrl(listItem):
    pat = re.compile(r'<a href=".*title="')
    url = pat.findall(listItem)
    endPos = len(url[0]) - 9
    return "www.pornhub.com" + url[0][9:endPos]


def getPornImgUrl(listItem):
    pat = re.compile(r'data-mediumthumb=".{0,150}\.jpg"')
    url = pat.findall(listItem)
    endPos = len(url[0]) - 1
    return url[0][18:endPos]


def insertDb(listItem, conn):
    title = getPornTitle(listItem)
    url = getPornUrl(listItem)
    img = getPornImgUrl(listItem)
    _time = str(int(time.time()))
    titles = conn.cursor().execute("SELECT TITLE from VIDEOS")
    for row in titles:
        if row[0].encode("utf-8") == title:
            return
    sql = "INSERT OR IGNORE INTO VIDEOS(TITLE,URL,IMG,TIME) VALUES ('" + title + "','" + url + "','" + img + "','" + _time + "')".encode(
        "utf-8")
    conn.cursor().execute(sql);


def getPorn(keyword, pages, conn):
    for page in range(1, pages + 1):
        print "getting about " + keyword + " page " + str(page) + '\n'
        pornPage = getPornPage(keyword, page)
        pornList = getPornList(pornPage)
        limit = 0
        for item in pornList:
            insertDb(item, conn)
            limit = limit + 1
            if limit == 2:
                return


def getPornWithKeywords(conn):
    words = conn.cursor().execute("SELECT * from KEYWORD order by rowid desc limit 10")
    for keys in words:
        getPorn(keys[0].encode("utf-8"), 1, conn)


def autoReflesh():
    conn = sqlite3.connect('porn.db')
    c = conn.cursor()
    c.execute('''
    	CREATE TABLE IF NOT EXISTS VIDEOS
       (TITLE   TEXT,
        URL     TEXT,
        IMG     TEXT,
        TIME    INTEGER);
    ''')
    c.execute('''
    	CREATE TABLE IF NOT EXISTS KEYWORD
       (WORDS   TEXT);
    ''')
    while 1 == 1:
        getPornWithKeywords(conn)
        conn.commit()
        print "reflesh!"
        time.sleep(60)
    conn.close()


class TestHTTPHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        conn = sqlite3.connect('porn.db')
        c = conn.cursor()
        c.execute('''
    	    CREATE TABLE IF NOT EXISTS VIDEOS
           (TITLE   TEXT,
            URL     TEXT,
            IMG     TEXT,
            TIME    INTEGER);
        ''')
        c.execute('''
        	CREATE TABLE IF NOT EXISTS KEYWORD
           (WORDS   TEXT);
        ''')
        sql = "SELECT * from VIDEOS ORDER BY TIME DESC LIMIT 20"
        videos = c.execute(sql);
        links = "<br>\n"
        kword = "<p>"
        for video in videos:
            links = links + '\n<p>' + video[0].encode("utf-8") + '</p>\n<br>\n<a href="https://' + video[1].encode(
                "utf-8") + '"><img src="' + video[2].encode("utf-8") + '"/></a>\n<br>'
        words = conn.cursor().execute("SELECT * FROM KEYWORD ORDER BY rowid DESC LIMIT 10")
        for keys in words:
            kword = kword + keys[0] + "<br>\n"
        conn.close()
        kword = kword + "</p>"
        templateStr = '''
        <html>   
          <head>   
            <title>what you like the newest 20 </title>   
          </head>   
          <body>
            <form action="httpserver.py" method="post">
              Add keyword:
              <br><br>
              <input type="text" name="keyword" value="what you like">
              <br><br>
              <input type="submit" value="add keyword">
            </form>
            <br><br>
            <p>keywords:</p>
            %s
            <br><br> 
            %s
          </body>   
        </html>
        ''' % (kword, links)
        self.protocal_version = 'HTTP/1.1'
        self.send_response(200)
        self.send_header("Welcome", "Contect")
        self.end_headers()
        self.wfile.write(templateStr)

    def do_POST(self):
        path = self.path
        datas = self.rfile.read(int(self.headers['content-length']))
        datas = urllib.unquote(datas).decode("utf-8", 'ignore')
        conn = sqlite3.connect('porn.db')
        c = conn.cursor()
        c.execute('''
    	    CREATE TABLE IF NOT EXISTS VIDEOS
           (TITLE   TEXT,
            URL     TEXT,
            IMG     TEXT,
            TIME    INTEGER);
        ''')
        c.execute('''
        	CREATE TABLE IF NOT EXISTS KEYWORD
           (WORDS   TEXT);
        ''')
        conn.close()
        templateStr = '''
        <html>   
          <head>   
            <title>what you like the newest 20 </title>   
          </head>   
          <body>
            <script language="javascript">
              window.location = "http://www.fanghr.top:4321";
           </script>
          </body>   
        </html>
        '''
        self.protocal_version = 'HTTP/1.1'
        self.send_response(200)
        self.send_header("Welcome", "Contect")
        self.end_headers()
        self.wfile.write(templateStr)
        keyword = datas[8:].encode("utf-8")
        conn = sqlite3.connect('porn.db')
        c = conn.cursor()
        c.execute('''
        	CREATE TABLE IF NOT EXISTS KEYWORD
           (WORDS   TEXT);
        ''')
        words = c.execute("SELECT WORDS FROM KEYWORD")
        for word in words:
            if word[0].encode("utf-8") == keyword:
                return
        sql = "INSERT OR IGNORE INTO KEYWORD (WORDS) VALUES ('" + keyword + "')"
        c.execute(sql);
        conn.commit()
        conn.close()


def start_server(port):
    http_server = HTTPServer(('', int(port)), TestHTTPHandler)
    http_server.serve_forever()


if __name__ == '__main__':
    try:
        daemonize("/daemonize.log",)
        thread.start_new_thread(start_server, (4321,))
        thread.start_new_thread(autoReflesh, ())
    except Exception, arg:
        print arg
    while 1:
        pass
